//
//  XHEditxiangViewController.m
//  Big Show
//
//  Created by 赵晓航 on 15-5-15.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "XHEditxiangViewController.h"
#import "XHEditCollectionViewCell.h"
#import "XHEditXiangObject.h"
#import "XHNetworkHandle.h"

@interface XHEditxiangViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) NSMutableArray *mArr;
@property (nonatomic, retain) NSMutableDictionary *mDic;
@property (nonatomic, retain) UIWebView *web;


@end

@implementation XHEditxiangViewController

- (void)dealloc
{
    [_mArr release];
    [_mDic release];
    [_web release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    
    [self getData];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    

#pragma mark - 收藏按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"喜爱1"] style:UIBarButtonItemStylePlain target:self action:@selector(Preserve)] autorelease];

}

// webView上的链接不能点击跳转
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        return NO;
    }
    return YES;
}






// 收藏按钮
-(void)Preserve
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 创建一个文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 添加文件夹
    // 拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"kof"];
    // 创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@", filePath);
    NSString *strPath = [filePath stringByAppendingPathComponent:@"kof.duang"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
    
    if (_myDic == nil) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
    }else{
        
        
        if (arr == nil) {
            arr = [NSMutableArray array];
            [arr addObject:_myDic];
            // 归档
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        } else {
            int a = 0;
            
            for (NSDictionary *dic in arr) {
                if ([[_myDic objectForKey:@"title"] isEqualToString:[dic objectForKey:@"title"]]) {
                    a = 1;
                }
            }
            if (a == 0) {
                [arr addObject:_myDic];
                [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];
            }
        }
        
        
        
    }
    
    
}



- (void)getData
{
    
    NSString *docid = [NSString stringWithFormat:@"http://a1.go2yd.com/Website/contents/content?appid=dress&bottom_channels=true&bottom_comments=true&cv=1.1.1&distribution=com.apple.appstore&docid=%@&highlight=true&net=wifi&platform=0&related_docs=true&version=010904", _docid];
    
    [XHNetworkHandle getDataWithURL:docid cookie:@"JSESSIONID=7tFm_0dR9sFG5Bm0IAnlqw" completion:^(id result) {
        // result为请求接口的返回数据
        self.mArr = [result objectForKey:@"documents"];
        self.mDic = result;
        [_web loadHTMLString:[[_mArr objectAtIndex:0]objectForKey:@"content"] baseURL:nil];
        
    }];
    
    [self createWebView];
    
}


- (void) createWebView
{
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113)];
    _web.scrollView.bounces = NO;
    [self.view addSubview:_web];
    [_web release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
